#include "SpriteInstance.h"

bool SpriteInstance::hasXY(Sint16 x, Sint16 y)
{
    if( !(x < m_posRect.m_rect.x || x > m_posRect.m_x2 || y < m_posRect.m_rect.y || y > m_posRect.m_y2) ) {
        return true;
    } else {
        if (Debug::test(DEBUG_INFO)) {
            cout << x << " " << y << " not in " << m_posRect.m_rect.x << " " << m_posRect.m_x2 << " " <<
                m_posRect.m_rect.y << " " << m_posRect.m_y2 << endl;
        }
    }
    return false;
}

void SpriteInstance::movebyXY(Sint16 x, Sint16 y)
{
    m_posRect.m_rect.x += x;
    m_posRect.m_rect.y += y;

    m_posRect.m_x2 = m_posRect.m_rect.x + m_posRect.m_rect.w;
    m_posRect.m_y2 = m_posRect.m_rect.y + m_posRect.m_rect.h;
}

void SpriteInstance::draw(SDL_Surface *layer)
{
    ImageList &img = *(ImageList::get_instance());
    ImageListItem &image = img[m_imgId];

    Image_Wrapper &src_img = *(image.m_pImg);
    SpriteListItem &sprite = image.m_spriteList[m_spriteId];
    if (Debug::test(DEBUG_DRAW)) {
        cout << "Drawing sprite at: " << m_posRect.m_rect.x << " " << m_posRect.m_rect.y << " " <<
            m_posRect.m_rect.w << " " << m_posRect.m_rect.h << endl;
    }
    SDL_Rect r = m_posRect.m_rect;
    SDL_BlitSurface(src_img.image, &sprite.m_rect, layer, &r);
}

bool SpriteInstance::overLaps(SpriteInstance &si)
{
    if(Debug::test(DEBUG_INFO)) {
        cout << m_posRect.m_rect.x << " " << m_posRect.m_rect.y << " "
            << m_posRect.m_x2 << " " << m_posRect.m_y2 << endl;
        cout << si.m_posRect.m_rect.x << " " << si.m_posRect.m_rect.y << " "
            << si.m_posRect.m_x2 << " " << si.m_posRect.m_y2 << endl;
    }
    if (si.m_posRect.m_rect.x < m_posRect.m_rect.x && si.m_posRect.m_x2 < m_posRect.m_rect.x) {
        return false;
    }

    if (si.m_posRect.m_rect.x > m_posRect.m_x2 && si.m_posRect.m_x2 > m_posRect.m_x2) {
        return false;
    }

    if (si.m_posRect.m_rect.y < m_posRect.m_rect.y && si.m_posRect.m_y2 < m_posRect.m_rect.y) {
        return false;
    }

    if (si.m_posRect.m_rect.y > m_posRect.m_y2 && si.m_posRect.m_y2 > m_posRect.m_y2) {
        return false;
    }
    return true;
}

bool SpriteInstance::overLaps(PosRectInfo &posRect)
{
    cout << m_posRect.m_rect.x << " " << m_posRect.m_rect.y << " "
         << m_posRect.m_x2 << " " << m_posRect.m_y2 << endl;
    cout << posRect.m_rect.x << " " << posRect.m_rect.y << " "
         << posRect.m_x2 << " " << posRect.m_y2 << endl;
    if (posRect.m_rect.x < m_posRect.m_rect.x && posRect.m_x2 < m_posRect.m_rect.x) {
        return false;
    }

    if (posRect.m_rect.x > m_posRect.m_x2 && posRect.m_x2 > m_posRect.m_x2) {
        return false;
    }

    if (posRect.m_rect.y < m_posRect.m_rect.y && posRect.m_y2 < m_posRect.m_rect.y) {
        return false;
    }

    if (posRect.m_rect.y > m_posRect.m_y2 && posRect.m_y2 > m_posRect.m_y2) {
        return false;
    }

    return true;
}

